﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Adorners
{
    public class RelationAdorner : Adorner
    {
        // Be sure to call the base class constructor.
        /// <summary>
        /// 用以表示标题被选取状态的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入标题的“MainBorder”主子元素。</param>
        public RelationAdorner(UIElement adornedElement, Relation masterRelation)
            : base(adornedElement)
        {
            this.masterRelation = masterRelation;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
        }

        private Relation masterRelation;

        public Relation MasterRelation
        {
            get { return masterRelation; }
        }

        // A common way to implement an adorner's rendering behavior is to override the OnRender
        // method, which is called by the layout system as part of a rendering pass.
        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect adornedElementRect = new Rect(this.AdornedElement.RenderSize);

            #region 先画线。
            Pen linePen = null;

            if (this.masterRelation != null && this.masterRelation.Editor != null)
            {
                linePen = new Pen(Brushes.Blue, 1);
                //linePen = this.masterTitle.Editor.SubPanelSelectionAdornerPen;
                linePen.DashStyle = DashStyles.DashDot;


                linePen.LineJoin = PenLineJoin.Round;
                linePen.StartLineCap = PenLineCap.Round;
                linePen.EndLineCap = PenLineCap.Round;

                Point newTopLeft = new Point(adornedElementRect.TopLeft.X - 2, adornedElementRect.TopLeft.Y - 2);
                Point ptTmp = new Point(adornedElementRect.BottomRight.X + 3, adornedElementRect.BottomRight.Y + 3);
                Rect lineRect = new Rect(newTopLeft, ptTmp);

                //使用这个办法其实比画四条线更好。但要注意，第一个参数必须为：null而不能是：Brshes.Transparent。
                //否则画出来的矩形不是空心的。
                //由于公式需要嵌套，一旦矩形不是空心，则选中外层的公式后，
                //就不能再用鼠标选中内层的公式了。
                drawingContext.DrawRectangle(null, linePen, lineRect);
            }
            #endregion

            #region 再画点。
            //SolidColorBrush renderBrush;
            Brush renderBrush;
            Pen renderPen;

            if (this.masterRelation != null && this.masterRelation.Editor != null)
            {
                renderPen = new Pen(Brushes.Blue, 1);
                //renderPen = this.masterTitle.Editor.ExSelectionAdornerPen;


                renderBrush = Brushes.Blue;

                //renderBrush = this.masterTitle.Editor.ExSelectionAdornerBrush;
            }
            else
            {
                renderPen = null;
                renderBrush = null;
            }

            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Left - 4, adornedElementRect.Top - 4, 4, 4));//左上
            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Right + 1, adornedElementRect.Top - 4, 4, 4));//右上
            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Left - 4, adornedElementRect.Bottom + 1, 4, 4));//左下
            drawingContext.DrawRectangle(renderBrush/*Brushes.Black*/, renderPen,
                new Rect(adornedElementRect.Right + 1, adornedElementRect.Bottom + 1, 4, 4));//右下

            #endregion
        }
    }
}
